package Task_0106;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Dao extends DBUtils {

    private User u = new User();

    /**
     * 判断用户名和密码是否正确
     * @param userName
     * @param password
     * @return
     */
    public boolean login(String userName, String password) {
        List<Object> paramList = new ArrayList<>();//存查询参数
        String sql = "select * from user where userName=?";
        paramList.add(userName);
        resultSet = query(sql, paramList);
        try {
            while (resultSet.next()) {
                //判断密码是否正确
                if (password.equals(resultSet.getString("password"))) {
                    return true;
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return false;
    }

    /**
     * 用户名有效性异步验证（查询用户名是否存在）
     * @param userName
     * @return
     */
    /*public boolean isExist(String userName) {
        List<Object> paramList = new ArrayList<>();//存查询参数
        String sql = "select * from user where userName=?";
        paramList.add(userName);
        resultSet = query(sql, paramList);
        try {
            while (resultSet.next()) {
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return false;
    }*/

    /**
     * 根据用户名自动填充其它信息
     * @param userName
     * @return
     */
    /*public User autoFill(String userName){
        List<Object> paramList = new ArrayList<>();//存查询参数
        String sql = "select * from user where userName=?";
        paramList.add(userName);
        resultSet = query(sql, paramList);
        try {
            while (resultSet.next()) {
                u.setUserName(resultSet.getString("userName"));
                u.setPassword(resultSet.getString("password"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return u;
    }*/

}
